﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common.Logging;
using WuDada.Core.Post;
using WuDada.Core.Post.Domain;
using WuDada.Core.Post.Service;
using WuDada.Core.SystemApplications.Domain;
using System.Collections.Generic;
using WuDada.Core.Member.Domain;
using WuDada.Core.Member;
using WuDada.Core.Member.Service;

public partial class admin_UC08_0822 : System.Web.UI.Page
{
    private ILog m_Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private PostFactory m_PostFactory;
    private IAnswerService m_AnswerService;
    private MemberFactory m_MemberFactory;
    private IMemberService m_MemberService;
    private WebLogService m_WebLogService;

    //加入我們分類 NodeId=38
    private int m_NodeId = 38;

    private int m_Mode
    {
        get { if (ViewState["mode"] == null) { ViewState["mode"] = 0; } return int.Parse(ViewState["mode"].ToString()); }
        set { ViewState["mode"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        m_WebLogService = new WebLogService();
        m_PostFactory = new PostFactory();
        m_AnswerService = m_PostFactory.GetAnswerService();
        m_MemberFactory = new MemberFactory();
        m_MemberService = m_MemberFactory.GetMemberService();

        if (!IsPostBack)
        {
            pnlContent.Visible = false;
            btnSearch_Click(null, null);
            ShowMode();
        }
    }

    private void ShowMode()
    {
        if (m_Mode == 0)
        {
            btnSave.Visible = false;
        }
        else
        {
            btnSave.Visible = true;
        }
    }

    private void fillGridView()
    {
        IDictionary<string, string> conditions = new Dictionary<string, string>();
        conditions.Add("KeyWord", txtKeyWord.Text.ToString());
        conditions.Add("MemberKeyWord", txtMemberKeyWord.Text.ToString());
        conditions.Add("Flag", "1");
        conditions.Add("Result", ddlSearchResult.SelectedValue);   
        conditions.Add("ApplyForVerification", ddlSearchApplyForVerification.SelectedValue);
        conditions.Add("ApplyForVerificationStatus", ddlSearchApplyForVerificationStatus.SelectedValue);   
        conditions.Add("Order", "order by a.CreatedDate desc ");

        //分頁
        AspNetPager1.RecordCount = m_AnswerService.GetAnswerCount(conditions);
        conditions.Add("PageIndex", (AspNetPager1.CurrentPageIndex - 1).ToString());
        conditions.Add("PageSize", AspNetPager1.PageSize.ToString());

        gvList.DataSource = m_AnswerService.GetAnswerList(conditions);
        gvList.DataBind();
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        fillGridView();
    }    

    private void ClearUI()
    {
        UIHelper.ClearUI(pnlContent);
        m_Mode = 0;
        ddlApplyForVerificationStatus.SelectedValue = "1";
        pnlContent.Visible = false;
    }

    protected void gvList_RowCommand1(object sender, GridViewCommandEventArgs e)
    {
        string cmdName = e.CommandName;
        int answerId = int.Parse(e.CommandArgument.ToString());
        AnswerVO answerVO = m_AnswerService.GetAnswerById(answerId);
        switch (cmdName)
        {
            case "myModify":
                ClearUI();
                UIHelper.FillUI(pnlContent, answerVO);
                m_Mode = answerId;
                MemberVO memberVO = m_MemberService.GetMemberById(answerVO.Member.MemberId);
                lblMemberName.Text = memberVO.Name;
                lblLoginId.Text = memberVO.LoginId;
                lblCreateDate.Text = answerVO.CreatedDate.ToString();
                
                if (answerVO.ApplyForVerification)
                {
                    ddlApplyForVerificationStatus.Enabled = true;
                }
                else
                {
                    ddlApplyForVerificationStatus.Enabled = false;
                }

                if (1.Equals(answerVO.Result))
                {
                    lblResult.Text = "及格";
                }
                else
                {
                    lblResult.Text = "不及格";
                }

                if (answerVO.ApplyForVerification)
                {
                    lblApplyForVerification.Text = "是";
                }
                else
                {
                    lblApplyForVerification.Text = "否";
                }

                ShowMode();
                pnlContent.Visible = true;
                btnSearch.Enabled = false;
                txtKeyWord.Enabled = false;
                txtMemberKeyWord.Enabled = false;
                ddlSearchResult.Enabled = false;
                ddlSearchApplyForVerification.Enabled = false;
                ddlSearchApplyForVerificationStatus.Enabled = false;
                break;
            case "myDel":
                m_AnswerService.DeleteAnswer(answerVO);
                m_WebLogService.AddSystemLog(MsgVO.Action.刪除, answerVO);
                break;

            default:
                break;
        }
        btnSearch_Click(null, null);
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        ClearUI();
        ShowMode();
        btnSearch.Enabled = true;
        txtKeyWord.Enabled = true;
        txtMemberKeyWord.Enabled = true;
        ddlSearchResult.Enabled = true;
        ddlSearchApplyForVerification.Enabled = true;
        ddlSearchApplyForVerificationStatus.Enabled = true;
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        fillGridView();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        AnswerVO answerVO = m_AnswerService.GetAnswerById(m_Mode);
        answerVO.ApplyForVerificationStatus = int.Parse(ddlApplyForVerificationStatus.SelectedValue.Trim());
        WebLogService.SetUpdateInfo(answerVO);
        m_AnswerService.UpdateAnswer(answerVO);
        m_WebLogService.AddSystemLog(MsgVO.Action.修改, answerVO);
        fillGridView();
        ClearUI();
        ShowMode();
        btnSearch.Enabled = true;
        txtKeyWord.Enabled = true;
        txtMemberKeyWord.Enabled = true;
        ddlSearchResult.Enabled = true;
        ddlSearchApplyForVerification.Enabled = true;
        ddlSearchApplyForVerificationStatus.Enabled = true;
    }

    protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        GridView gv = (GridView)sender;
        if (e.Row.RowIndex != -1)
        {
            Control ctrl = e.Row;
            int answerId = int.Parse(UIHelper.FindHiddenValue(ref ctrl, "hdnAnswerId"));
            AnswerVO answerVO = m_AnswerService.GetAnswerById(answerId);

            if (1.Equals(answerVO.Result))
            {
                UIHelper.SetLabelText(ref ctrl, "lblResult", "及格");
            }
            else
            {
                UIHelper.SetLabelText(ref ctrl, "lblResult", "不及格");
            }

            if (answerVO.ApplyForVerification)
            {
                UIHelper.SetLabelText(ref ctrl, "lblApplyForVerification", "是");
            }
            else
            {
                UIHelper.SetLabelText(ref ctrl, "lblApplyForVerification", "否");
            }

            if (1.Equals(answerVO.ApplyForVerificationStatus))
            {
                UIHelper.SetLabelText(ref ctrl, "lblApplyForVerificationStatus", "已處理");
            }
            else if (2.Equals(answerVO.ApplyForVerificationStatus))
            {
                UIHelper.SetLabelText(ref ctrl, "lblApplyForVerificationStatus", "未處理");
            }

            MemberVO memberVO = m_MemberService.GetMemberById(answerVO.Member.MemberId);
            UIHelper.SetLabelText(ref ctrl, "lblMemberName", memberVO.Name);
        }
    }

    protected void btnShowAdd_Click(object sender, EventArgs e)
    {
        ClearUI();
        ShowMode();
        pnlContent.Visible = true;
        btnSearch.Enabled = false;
    }
}